/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.logistics.service.inboundorder;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.logistics.dao.inboundorder.InboundOrderDao;
import com.jeesite.modules.logistics.entity.inboundorder.InboundOrder;
import com.jeesite.modules.logistics.entity.outboundorder.OutboundOrder;
import com.jeesite.modules.logistics.service.outboundorder.OutboundOrderService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库单Service
 * @author LV
 * @version 2021-03-10
 */
@Service
@Transactional(readOnly=true)
public class InboundOrderService extends CrudService<InboundOrderDao, InboundOrder> {

    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private OutboundOrderService outboundOrderService;
    @Autowired
    private ServiceOrderService serviceOrderService;
	/**
	 * 获取单条数据
	 * @param inboundOrder
	 * @return
	 */
	@Override
	public InboundOrder get(InboundOrder inboundOrder) {
		return super.get(inboundOrder);
	}
	
	/**
	 * 查询分页数据
	 * @param inboundOrder 查询条件
	 * @param inboundOrder.page 分页对象
	 * @return
	 */
	@Override
	public Page<InboundOrder> findPage(InboundOrder inboundOrder) {
		return super.findPage(inboundOrder);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param inboundOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(InboundOrder inboundOrder) {
        if (inboundOrder.getIsNewRecord()){
            inboundOrder.setInboundOrderCode(codeRuleService.getCode("inboundorder"));
        }
		super.save(inboundOrder);
        if (inboundOrder.getIsNewRecord()){
            codeRuleService.updateCurrentNumber("inboundorder");
        }
		// 保存上传附件
		FileUploadUtils.saveFileUpload(inboundOrder.getId(), "inboundOrder_file");

        // 回写出库单
        OutboundOrder outboundOrder = outboundOrderService.get(inboundOrder.getOutboundOrderId());
        outboundOrder.setInboundOrderCode(inboundOrder.getInboundOrderId());
        outboundOrderService.save(outboundOrder);

        // 回写服务订单
        ServiceOrder serviceOrder = serviceOrderService.get(inboundOrder.getServiceOrderId());
        for (ServiceOrderDetail serviceOrderDetail : serviceOrder.getServiceOrderDetailList()) {
            if (serviceOrderDetail.getFrameNumber().equals(inboundOrder.getFrameNumber())){
                serviceOrderDetail.setWarehouseId(inboundOrder.getToWarehouse());
            }
        }
        serviceOrderService.save(serviceOrder);
    }
	
	/**
	 * 更新状态
	 * @param inboundOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(InboundOrder inboundOrder) {
		super.updateStatus(inboundOrder);
	}
	
	/**
	 * 删除数据
	 * @param inboundOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(InboundOrder inboundOrder) {
		super.delete(inboundOrder);
	}
	
}